package com.mch.func.web.browser;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.w3c.dom.Document;

import javafx.beans.value.ChangeListener;
import javafx.concurrent.Worker.State;

public class TestBrowser extends JFrame {

	private JPanel contentPane;
	private BrowserPanel panel;
	private JPanel panel_1;
	private JButton btnNewButton;
	private JButton btnNewButton_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					TestBrowser jf = new TestBrowser();
					jf.getPanel().loadURL("https://wenku.baidu.com/view/d7e10826482fb4daa58d4bc5.html?tdsourcetag=s_pcqq_aiomsg&qq-pf-to=pcqq.c2c");
					jf.setPreferredSize(new Dimension(1024, 600));
					jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					jf.pack();

					jf.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TestBrowser() {
		initGUI();
	}

	private void initGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 872, 493);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.add(getPanel(), BorderLayout.CENTER);
		contentPane.add(getPanel_1(), BorderLayout.NORTH);
	}

	public BrowserPanel getPanel() {
		if (panel == null) {
			panel = new BrowserPanel();
		}
		return panel;
	}

	public JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.add(getBtnNewButton());
			panel_1.add(getBtnNewButton_1());
		}
		return panel_1;
	}

	public JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("New button");
			btnNewButton.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					String str = getPanel().getEngine().getDocument().getTextContent();
					System.out.println(str);
					// Object o =
					// getPanel().getEngine().executeScript("document.documentElement.outerHTML");
					// System.out.println(o);

					// getPanel().getEngine().getLoadWorker().
				}
			});
		}
		return btnNewButton;
	}

	public JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("New button");
		}
		return btnNewButton_1;
	}
}
